pp108 : Process Platform Mobile Plug-in

Process Platform Mobile Plug-in

The cordys.mobile plug-in is used to access the native features of a mobile phone from an HTML page. It can be used for activities such as capturing a picture, beeping, and vibrating. This plug-in only works when the page is accessed using the Native App, running on Phone Gap.

The cordys.mobile plug-in is not a part of the Process Platform HTML5 SDK script. Therefore, you must include the Process Platform HTML5 SDK script and the cordys.mobile script by adding the following command in your code:

<script src="/cordys/html5/plugins/cordys.mobile.js" type="text/javascript"/>

Methods

All the methods and definitions must be prefixed with $.cordys.mobile. The following methods are supported by the cordys.mobile plug-in:

Table 1. List of Methods

Method

Description

PhoneGap API

camera.getPicture(successCallback, errorCallback, options)

Captures a photo using the camera or retrieves a photo from the album in the device

getPicture

fileReader.readAsDataURL(filePath, successCallback, errorCallback)

Reads a file and returns data as a base64 encoded data URL

readAsDataURL

fileReader.readAsText(filePath, encoding, successCallback, errorCallback)

Reads a text file

readAsText

notification.alert(message, alertCallback, title, buttonName)

Displays a custom alert or a dialog box

alert

notification.confirm(message, confirmCallback, title, buttonLabels)

Displays a confirmation dialog box, which can be customized

confirm

notification.beep(times)

Plays a beep sound on the device

beep

notification.vibrate(milliseconds)

Vibrates the device for a specific duration

vibrate

environment.getEnvironment()

Retrieves the following details:

    • Device: Device model, platform, version, and uuid
    • Connection: Connection type and status
    • Server: ServerId - Index of the server
    • isMobile: Returns true only if Cordova is loaded

 

 
environment.getConnectionInfo() Retrieves the connection type and status Connection
geolocation.getCurrentPosition() Retrieves the current location of the user. geolocation
geolocation.subscribeWatchPosition() Retrieves the new position of the user whenever it is getting changed.  
geolocation.unsubscribeWatchPosition() Stop watching the changes in the user position.  

Note:

  • All the methods have the same API as their representative in PhoneGap. You can find the full description of all options and parameters at the corresponding link in the PhoneGapAPI column.
  • Not all methods supported by PhoneGap are available yet, but they can be added on request.
Table 2. List of Defines

Define

Description

DEFAULT

camera.DestinationType.DATA_URL

Returns an image as a base64 encoded string and used in the destinationType option

DEFAULT

camera.DestinationType.FILE_URL

Returns an image file URL and used in the destinationType option

 

camera.PictureSourceType.PHOTOLIBRARY

Sets the source of the picture to the photo library and used in the sourceType option

 

camera.PictureSourceType.CAMERA

Sets the source of the picture to the camera and used in the sourceType option

DEFAULT

camera.PictureSourceType.SAVEDPHOTOALBUM

Sets the source of the picture saved to the photo album and used in the sourceType option

 

camera.EncodingType.JPEG

Returns a JPEG encoded image and used in the encodingType option

DEFAULT

camera.EncodingType.PNG

Returns a PNG encoded image and used in the encodingType option

 

camera.MediaType.PICTURE

Allows the selection of still pictures and works only when the sourceType is PHOTOLIBRARY or SAVEDPHOTOALBUM. It is used in the mediaType option.

DEFAULT

camera.MediaType.VIDEO

Allows the selection of video and works when the sourceType is PHOTOLIBRARY or SAVEDPHOTOALBUM. It is used in the mediaType option.

 

camera.MediaType.ALLMEDIA

Allows the selection of all the media types and works when the sourceType is PHOTOLIBRARY or SAVEDPHOTOALBUM. It is used in the mediaType option.

 

Examples

The following are the the sample code snippets:

Capture Picture
var options = {
    quality: 25,
    destinationType: $.cordys.mobile.camera.DestinationType.FILE_URI
}
$.cordys.mobile.camera.getPicture(onSuccess, onError, options);
 
function onSuccess(parameters) {
    document.getElementById("imgPhoto").src = parameters.imageData;
}
function onError(parameters) {
    alert("Failed to take picture: " + parameters.message);
}
Upload Picture
var options = {
    sourceType: $.cordys.mobile.camera.PictureSourceType.PHOTOLIBRARY,
    destinationType: $.cordys.mobile.camera.DestinationType.DATA_URL,
    mediaType: $.cordys.mobile.camera.MediaType.PICTURE
}
$.cordys.mobile.camera.getPicture(onSuccess, onError, options);
 
function onSuccess(parameters) {
    document.getElementById("imgPhoto").src = "data:image/jpeg;base64," + parameters.imageData;
}
function onError(parameters) {
    alert("Failed to get picture: " + parameters.message);
} 
Vibrate Mobile
 $.cordys.mobile.notification.vibrate(1000); 
Beep Mobile
 $.cordys.mobile.notification.beep(2); 
Connection Details
$.cordys.mobile.getEnvironment(onSuccess, onError);
  
function onSuccess(environment) {
    //device details
    var model = environment.device.model,
        platform = environment.device.platform,
        version = environment.device.version,
        uuid = environment.device.uuid;
 
    //connection details
    var connection_type = environment.connection.type,
       connection.isOnline = environment.connection.isOnline;
 
    //server details
    var serverId = environment.server.serverId;
 
    //isMobile
    var isMobile = environment.isMobile;
 }
 
function onError() {
    alert("Failed to retrieve the environment details");
}
Device Information
$.cordys.mobile.environment.getConnectionInfo(onSuccess, onError);
  
function onSuccess(connection) {
   var connection_type = connection.type,
       connection.isOnline = connection.isOnline;
}
 
function onError() {
    alert("Failed to retrieve Connection details");
}
Geolocation
$.cordys.mobile.geolocation.getCurrentPosition(function(position) {  
		alert('Latitude: '          + position.coords.latitude          + '\n' +
			  'Longitude: '         + position.coords.longitude         + '\n' +
              'Altitude: '          + position.coords.altitude);
}, function(parameters) {
	alert("failed: " + parameters.message);
}, {
	maximumAge: 3000, timeout: 5000, enableHighAccuracy: true
});